//microcommand define savefile
const fs = require("fs")
signature("<path> <file:file>", { authority: 4, checkArgCount: true, strictOptions: true })
action(async ({ session }, path, fileAttrs) => {
  //const { file } = await session.onebot._get("get_file", { file_id: fileAttrs.fileId })
  const name = fileAttrs.title || fileAttrs.file_name || fileAttrs.file || "download"
  const isDir = (await fs.promises.stat(path).catch(() => null))?.isDirectory()
  if (isDir) path += "/" + name
  //await fs.promises.copyFile(file, path)
  const stream = await ctx.http.get(fileAttrs.src, { responseType: "stream" })
  const writer= fs.createWriteStream(path)
  require("stream").Readable.fromWeb(stream).pipe(writer)
  await new Promise((res, rej) => { writer.on("finish", res); writer.on("error", rej) })
  return session.i18n(".ok", [path])
})
locale("zh-CN", { description: "保存文件", arguments: { path: "保存位置", file: "文件" }, messages: { ok: "成功保存 {0}。" } })